﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Client.Models;

namespace Sautom.Modules.Client.ViewModels
{
    public class ClientIndexViewModel : NavigationAwareNotificationObject, IRegionMemberLifetime
    {
        #region Constructor
        public ClientIndexViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            ClientDetailsCommand = new DelegateCommand(ExecuteClientDetailsCommand, () => SelectedItem != null);
            AddClientCommand = new DelegateCommand(() => RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.ClientEdit));

            QueriesServiceClient = serviceFactory.GetQueriesService();
        }
        #endregion

        #region Tech properties

        public IQueriesService QueriesServiceClient { get; set; }
        #endregion

        #region Commands
        public DelegateCommand ClientDetailsCommand { get; private set; }
        public DelegateCommand AddClientCommand { get; private set; }

        private void ExecuteClientDetailsCommand()
        {
            var uriQuery = new UriQuery();
            if (SelectedItem != null)
            {
                uriQuery.Add("clientId", SelectedItem.Id.ToString());
            }

            var uri = new Uri(PathProvider.ClientDetails + uriQuery, UriKind.Relative);

            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }

        #endregion

        #region For View properties

        private IRegionManager RegionManager { get; set; }

        private string _personalNumberFilter;
        public string PersonalNumberFilter
        {
            get { return _personalNumberFilter; }
            set
            {
                _personalNumberFilter = value;
                UpdateModelFromService();
            }
        }

        private string _nameRuFilter;
        public string NameRuFilter
        {
            get { return _nameRuFilter; }
            set
            {
                _nameRuFilter = value;
                UpdateModelFromService();
            }
        }

        private string _courseNameFilter;
        public string CourseNameFilter
        {
            get { return _courseNameFilter; }
            set
            {
                _courseNameFilter = value;
                UpdateModelFromService();
            }
        }

        private ClientItemModel _selectedItem;
        public ClientItemModel SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                RaisePropertyChanged(() => SelectedItem);
                ClientDetailsCommand.RaiseCanExecuteChanged();
            }
        }

        private ObservableCollection<ClientItemModel> _clients;
        public ObservableCollection<ClientItemModel> Clients
        {
            get { return _clients; }
            set
            {
                _clients = value;
                RaisePropertyChanged(() => Clients);
            }
        }

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            UpdateModelFromService();
        }
        #endregion

        #region Implementation of IRegionMemberLifetime

        public bool KeepAlive
        {
            get { return true; }
        }

        #endregion

        #region Private members
        private void UpdateModelFromService()
        {
            Mapper.CreateMap<ClientItemDtoOutput, ClientItemModel>();

            var uiDispather = Dispatcher.CurrentDispatcher;
            (new Thread(() =>
            {
                List<ClientItemDtoOutput> clients = QueriesServiceClient.GetAllClients(PersonalNumberFilter, NameRuFilter, CourseNameFilter).ToList();
                uiDispather.BeginInvoke(DispatcherPriority.Normal,
                                        (Action)(() => Clients = Mapper.Map(clients, Clients)));
            })).Start();
        }
        #endregion
    }
}
